package cn.mabach.goods.service.impl;


import cn.mabach.DTO.PageUtils;
import cn.mabach.DTO.RS;
import cn.mabach.goods.dao.CategoryDao;
import cn.mabach.goods.entity.CategoryEntity;
import cn.mabach.goods.repository.CategoryRepository;
import cn.mabach.goods.service.CategoryService;
import cn.mabach.transaction.RedisDataSoureceTransaction;
import cn.mabach.utils.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static cn.mabach.consts.EhCache.*;

@RestController
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedisDataSoureceTransaction redisDataSoureceTransaction;




    @Override
    public RS<PageUtils> queryPage(@RequestParam Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>().like(StringUtils.isNotEmpty((String) params.get("key")),"name",params.get("key"))
        );
        if (page==null){
            return RS.error();
        }
        return RS.ok(new PageUtils(page));
    }





    @Override
    public RS<CategoryEntity> getByIdE(@RequestParam("id") Integer id) {
            CategoryEntity res = this.getById(id);
        if (res==null){
            return RS.error();
        }
        return RS.ok(res);
    }

//    删除ehcache和redis的缓存，并重新查询重新缓存
    @Override
    @CacheEvict(cacheNames = EHCACHE,key = "'categoryTreeCache'",beforeInvocation = true)
    public RS saveE(@RequestBody  CategoryEntity entity) throws Exception {

        TransactionStatus transactionStatus=null;
        try {
            transactionStatus = redisDataSoureceTransaction.begin();
            boolean b = this.save(entity);
            redisTemplate.boundHashOps(CATEGORYTREEREDIS).delete(CATEGORYTREEREDISKEY);
            redisDataSoureceTransaction.commit(transactionStatus);
        } catch (Exception e) {
            e.printStackTrace();
            redisDataSoureceTransaction.rollback(transactionStatus);
            return RS.error();
        }
        categoryRepository.getCategoryTreeFromCache();
        return RS.ok();
    }

    //    删除ehcache和redis的缓存，并重新查询重新缓存
    @Override
    @CacheEvict(cacheNames = EHCACHE,key = "'categoryTreeCache'",beforeInvocation = true)
    public RS updateByIdE(@RequestBody CategoryEntity entity) throws Exception {

        TransactionStatus transactionStatus=null;
        try {
             transactionStatus = redisDataSoureceTransaction.begin();
            boolean b = this.updateById(entity);
            redisTemplate.boundHashOps(CATEGORYTREEREDIS).delete(CATEGORYTREEREDISKEY);
            redisDataSoureceTransaction.commit(transactionStatus);
        } catch (Exception e) {
            e.printStackTrace();
            redisDataSoureceTransaction.rollback(transactionStatus);
            return RS.error();
        }
        categoryRepository.getCategoryTreeFromCache();
        return RS.ok();
    }


    //    删除ehcache和redis的缓存，并重新查询重新缓存
    @Override
    @CacheEvict(cacheNames = EHCACHE,key = "'categoryTreeCache'",beforeInvocation = true)
    public RS removeByIdsE(@RequestBody List<Integer> ids) throws Exception {
        TransactionStatus transactionStatus=null;
        try {
            transactionStatus  = redisDataSoureceTransaction.begin();
            boolean b = this.removeByIds(ids);
            redisTemplate.boundHashOps(CATEGORYTREEREDIS).delete(CATEGORYTREEREDISKEY);
            redisDataSoureceTransaction.commit(transactionStatus);
        } catch (Exception e) {
            e.printStackTrace();
            redisDataSoureceTransaction.rollback(transactionStatus);
            return RS.error();
        }
        categoryRepository.getCategoryTreeFromCache();

        return RS.ok();
    }

    @Override
    public RS<List<Map>> findCategoryTree() {
        List<Map> categoryTree = categoryRepository.getCategoryTreeFromCache();

        return RS.ok(categoryTree);
    }

}